﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using CMN.Controllers;
using CMN.Controllers.Filters;
using CMN.Common;
using System.Text;
using CMN.UI;
using CMN.Framework;
using CMN.Config;
namespace CMN.Controllers
{
   
    [LoggerFilter()]
    [ExceptionFilter()]
    [ExecutionTimingAttribute()]
    public class ZTController : Controller
    {
        [CompressFilter]
        [CacheFilterAttribute]
        public string Index(string dir)
        {

            string path = dir;
            PageHelperEntity pageHelper = new PageHelperEntity();
         
            try
            {
                Utils.GetQueryAndFormString(dir, 50, 1, DataType.CharAndNum);
            }
            catch
            {
                Utils.Redirect404();
                return "";
            }
            pageHelper.Url = dir;
            IDataService<Normal_ZTEntity> normal_ZTSv = ServiceBuilder.BuildNormal_ZTService();
            QueryParam qp = new QueryParam();
            qp.Where = string.Concat("dir='", dir, "'");
            qp.PageIndex = 1;
            qp.PageSize = 1;
            int count;
            List<Normal_ZTEntity> ztList = normal_ZTSv.List(qp, out  count);
            Normal_ZTEntity ztEntity = new Normal_ZTEntity();
            if (ztList == null || ztList.Count < 1)
            {
                Utils.Redirect404();
                return "";
            }
            else
            {
                ztEntity = ztList[0];
            }
            pageHelper.OtherEntity = ztEntity;
           
            pageHelper.Url = string.Concat("/zt/", ztEntity.Dir);
            pageHelper.ChannelId = ztEntity.Id;
            pageHelper.Title = (string.IsNullOrEmpty(ztEntity.SEOTitle) ? ztEntity.Title : ztEntity.SEOTitle);
            pageHelper.Description = ztEntity.Summary;
            pageHelper.Keywords = ztEntity.KeyWords;
            pageHelper.Type = PageType.OtherPage;
            pageHelper.OtherTypeName = "zt";
            SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
            if (siteInfo.IsHtml != 0)
            {

                string htmlPath = string.Concat(Utils.GetServerPath(), "App_Data\\html\\zt\\", ztEntity.Dir, ".htm");
                string content = string.Empty;
                if (Utils.FileIsOld(htmlPath, siteInfo.CacheSecond+3600))
                {
                    content = Utils.GetFileContent(htmlPath, Encoding.GetEncoding("gb2312"));
                }
                else
                {
                    content = getIndex(pageHelper);
                    Utils.CreateFile(htmlPath, content, null);
                }
                return content;
            }
            return getIndex(pageHelper);


        }
       private string getIndex(PageHelperEntity pageHelper)
       {
           BaseTemplateDisplay articleTemplate = new BaseTemplateDisplay(pageHelper);
           string strRetrun = articleTemplate.Display();
           articleTemplate.Dispose();
           return strRetrun;
       }   
              



        
    }
}
